/**
 * 仓库当月库存记录
 */
import { Application } from "egg";
import { ObjectId } from "mongoose";
import * as AutoIncrementFactory from "mongoose-sequence";

export interface WarehouseStockProps {
  _id: ObjectId; // 操作记录id
  warehouse: ObjectId; // 所属仓储
  amount: number; // 当前库存
  is_delete: boolean; // 是否删除
  createdAt: number;
  updatedAt: number;
}

function initWarehouseStockModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const WarehouseStockScheme = new Schema<WarehouseStockProps>(
    {
      warehouse: { type: Schema.Types.ObjectId, required: true },
      amount: { type: Number },
      is_delete: { type: Boolean, default: false },
      createdAt: { type: Number },
      updatedAt: { type: Number },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  WarehouseStockScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "Warehouse_stock_counter",
  });
  return mongoose.model<WarehouseStockProps>(
    "WarehouseStock",
    WarehouseStockScheme
  );
}

export default initWarehouseStockModel;
